//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    searchWhat: "",
    searchGoodsOrStore: "商品",
    searchId: '',
    // 上拉加载
    allGoodsData: [],
    pageIndex: 1,
    // 定位
    X: 0,
    Y: 0,
    uid: "",
    isArea: 0,
    businessId: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      searchWhat: options.searchWhat,
      searchGoodsOrStore: options.searchGoodsOrStore,
      searchId: options.searchId,
      businessId: options.businessId,
    })
    console.log(options.businessId)
    if (this.data.searchId == 0) {
      this.SerchGoods();
    } else {
      this.SerchBusiness();
    }
  },

  // 点击体验店进入店铺
  storeTap: function (e) {
    var businessId = e.currentTarget.dataset.id
    console.log(businessId);
    wx.navigateTo({
      url: '/packageA/store/store?businessId=' + businessId,
    })
  },

  // 上滑触底
  onReachBottom: function () {
    this.data.pageIndex++;
    if (this.data.searchId == 0) {
      this.SerchGoods();
    } else {
      this.SerchBusiness();
    }
  },
  // 搜索商品
  SerchGoods: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var uid = this.data.uid;
    var searchWhat = this.data.searchWhat;
    var allGoodsData = this.data.allGoodsData;
    var pageIndex = this.data.pageIndex;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=SerchGoods&UID=" + uid + "&City=&Area=&PageSize=10&KeyWord=" + searchWhat + "&PageIndex=" + pageIndex),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(json);
        for (var i = 0; i < json.length; i++) {
          allGoodsData.push(json[i])
        }
        that.setData({
          allGoodsData: allGoodsData,
        })
        console.log(allGoodsData);
        if(json.length==0){
          wx.showToast({
            title: '没有更多了',
            icon:'none',
            duration:1200
          })
        }
        wx.hideLoading();
      }
    })
  },
  // 搜索商家
  SerchBusiness: function () {
    if (app.isLogin()) {
      wx.showLoading({
        title: '加载中',
        icon: 'success',
        duration: 2000
      })
      var that = this;
      var latitude = this.data.latitude;
      var longitude = this.data.longitude;
      var uid = this.data.uid;
      var searchWhat = this.data.searchWhat;
      var allGoodsData = this.data.allGoodsData;
      var pageIndex = this.data.pageIndex;
      var X = this.data.X;
      var Y = this.data.Y;
      var businessId = this.data.businessId;
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=SerchBusiness&City=&Area=&PageSize=10&UID=" + uid + "&BusinessType=0&BaiduX=" + X + "&BaiduY=" + Y + "&SerchKey=" + searchWhat + "&PageIndex=" + pageIndex),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
          console.log(json);
          for (var i = 0; i < json.length; i++) {
            allGoodsData.push(json[i])
          }
          that.setData({
            allGoodsData: allGoodsData,
          })
          console.log(allGoodsData);
          console.log(json);
          if(json.length==0){
            wx.showToast({
              title: '没有更多了',
              icon:'none',
              duration:1200
            })
          }
          wx.hideLoading();
        }
      })
    }
  },
})